<?php

class IndexController extends Zend_Controller_Action {

    public function init() {
	if (!Zend_Auth::getInstance()->hasIdentity()) {
	    return $this->_helper->redirector->goToRoute(array('controller' => 'login'), null, true);
	}
    }

    public function indexAction() {
	$usuario = Zend_Auth::getInstance()->getIdentity();
	$this->view->usuario = $usuario;

	$senhas = new Application_Model_DbTable_Senha();
	$this->view->senhas = $senhas->fetchAll();

	$this->view->titulo = "Senhas";
    }

    public function adicionarAction() {
	$formulario = new Application_Form_Senha();
	$formulario->submit->setLabel('Adicionar');
	$this->view->titulo = "Adicionar nova senha";
	$this->view->formulario = $formulario;

	if ($this->getRequest()->isPost()) {
	    $formularioData = $this->getRequest()->getPost();
	    if ($formulario->isValid($formularioData)) {
		$artist = $formulario->getValue('artist');
		$title = $formulario->getValue('title');
		$albums = new Application_Model_DbTable_Senha();
		$albums->addAlbum($artist, $title);

		$this->_helper->redirector('index');
	    } else {
		$formulario->populate($formularioData);
	    }
	}
    }

}

